package com.godpaper.the3directs.busniess.managers
{
	import com.godpaper.the3directs.busniess.AI.*;
	import com.godpaper.the3directs.busniess.factorys.ButtonFactory;
	import com.godpaper.the3directs.model.BoardModel;
	import com.godpaper.the3directs.model.PiecesModel;
	import com.godpaper.the3directs.model.PlayerModel;
	
	import flash.display.DisplayObject;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	public class PlayerManager
	{
		public function PlayerManager()
		{
		}
		//try switch this slot to slecte all kinds of GameAI.:)
		//简化IAIBehavior接口,等待实例类实例化
		private static var gameAI:IAIBehavior;
		
		protected static function restartGame():void
		{
			for(var i:int=0;i<PiecesModel.getInstance().redPiecesCollection.length;i++)
			{
				ButtonFactory.chessBoard.removeChild(PiecesModel.getInstance().redPiecesCollection.getItemAt(i) as DisplayObject);
			}
			for(var j:int=0;j<PiecesModel.getInstance().bluePiecesCollection.length;j++)
			{
				ButtonFactory.chessBoard.removeChild(PiecesModel.getInstance().bluePiecesCollection.getItemAt(j) as DisplayObject);
			}
			
			BoardModel.getInstance().gamePosition.clear();
			BoardModel.getInstance().gameGraphBlue.clear();
			BoardModel.getInstance().gameGraphRed.clear();
			
			
			PiecesModel.getInstance().bluePiecesCollection.removeAll();
			PiecesModel.getInstance().selectedPiece = null;
			PiecesModel.getInstance().redPiecesCollection.removeAll();
			PiecesModel.getInstance().adjacentCollectionBlue.removeAll();
			PiecesModel.getInstance().oppositeCollectionBlue.removeAll();
			PiecesModel.getInstance().hypotenuseCollectionBlue.removeAll();
			PiecesModel.getInstance().adjacentCollectionRed.removeAll();
			PiecesModel.getInstance().oppositeCollectionRed.removeAll();
			PiecesModel.getInstance().hypotenuseCollectionRed.removeAll();
			
			ButtonFactory.produce(ButtonFactory.TYPE_PIECE,ButtonFactory.chessBoard,ButtonFactory.FLAG_RED);
    		ButtonFactory.produce(ButtonFactory.TYPE_PIECE,ButtonFactory.chessBoard,ButtonFactory.FLAG_BLUE);
		}
		private static function closeHandler(event:CloseEvent):void
		{
			if(event.detail == mx.controls.Alert.YES)
			{
				restartGame();
			}
		}
		public static function computerWin():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
//			mx.controls.Alert.show("COMPUTER WIN! TRY AGAIN?","CONGURATLATIONS!!!",Alert.YES|Alert.NO,null,closeHandler,null,Alert.YES);
			mx.controls.Alert.show("COMPUTER WIN!!!","CONGURATLATIONS!!!");
		}
		public static function humanWin():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
//			mx.controls.Alert.show("HUMAN WIN! TRY AGAIN?","CONGURATLATIONS!!!",Alert.YES|Alert.NO,null,closeHandler,null,Alert.YES);
			mx.controls.Alert.show("HUMAN WIN!!!","CONGURATLATIONS!!!");
		}
		
		public static function isComputerTurnNow():void
		{
			ProgressBarManager.getInstance().showProgressBar();
			PlayerModel.getInstance().gameTurnStatus = !PlayerModel.getInstance().gameTurnStatus;
			//todo:
//			gameAI = new RandomWalkAI(BoardModel.getInstance().gamePosition);
//			gameAI = new CounterAttackAI(BoardModel.getInstance().gamePosition);
			gameAI = new FalseAttactAI(BoardModel.getInstance().gamePosition);
		}
		public static function isHumanTurnNow():void
		{
			ProgressBarManager.getInstance().removeProgressBar();
			PlayerModel.getInstance().gameTurnStatus = !PlayerModel.getInstance().gameTurnStatus;
			//todo:
		}
	}
}	